package com.macrounion.atv.service.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macrounion.atv.service.entity.Net;
import com.macrounion.atv.service.entity.ext.ProtocolExt;
import com.macrounion.atv.service.entity.ext.ProtocolOutExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.macrounion.atv.service.entity.Protocol;
import com.macrounion.atv.service.entity.ProtocolC;
import com.macrounion.atv.service.mapper.ProtocolMapper;
import com.macrounion.atv.service.mapper.ProtocolMapperExt;
import com.macrounion.atv.service.service.ProtocolService;
import com.macrounion.base.service.service.impl.BaseServiceImpl;

@Service
public class ProtocolServiceImpl extends BaseServiceImpl<ProtocolMapper, Protocol> implements ProtocolService {

    @Autowired
    private ProtocolMapperExt protocolMapperExt;

    @Override
    public Map<String, List<ProtocolC>> getProtocolMap(List<String> pids) {
        List<ProtocolC> pcs = protocolMapperExt.getProtocolMap(pids);
        if (CollectionUtils.isEmpty(pcs))
            return null;
        Map<String, List<ProtocolC>> collect = pcs.stream()
                .collect(Collectors.groupingBy(ProtocolC::getProtocolId));
        return collect;
    }

    @Override
    public List<Net> getNets() {
        return protocolMapperExt.getNets();
    }

    @Override
    public Page<ProtocolExt> requestPageExt(Protocol p) {
        if (p != null && p.getPageInfo() != null) {
            PageHelper.startPage(p.getPageInfo().getPageIndex(), p.getPageInfo().getPageSize());
            return (Page) protocolMapperExt.requestPageExt(p);
        } else {
            return null;
        }
    }

    @Override
    public List<ProtocolOutExt> getProtocolOut(String protocolId) {
        return protocolMapperExt.getProtocolOut(protocolId);
    }

    @Override
    public int saveProtocols(List<Protocol> protocols) {
        return protocolMapperExt.saveProtocols(protocols);
    }

    @Override
    public int getCountByName(String protocolName) {
        return protocolMapperExt.getCountByName(protocolName);
    }

    @Override
    public boolean checkIsUsed(String protocolId) {
        int resultCount = protocolMapperExt.checkIsUsed(protocolId);
        return resultCount > 0;
    }

    @Override
    public List<Protocol> getProtocolByType(String type) {
        return protocolMapperExt.getProtocolByType(type);
    }

    @Override
    public List<Protocol> getProtocolByNetId(String netId) {

        return protocolMapperExt.getProtocolByNetId(netId);
    }
}
